/**
 * Created by thinkl on 2017/6/21.
 */
$(function () {

    var commodity_id = getQueryString('id');
    $.post('/commodity/get_commodity', {commodity_id: commodity_id}, function (data) {
        var l = JSON.parse(data);
        var res = l.result;
        $('#mod_name').val(res.name);
        $('#mod_price').val(res.price);
        $('#mod_preferential').val(res.preferential);
        var html = '';
        $.post('/commodity/query_commodity_class', {}, function (data) {
            var l2 = JSON.parse(data);
            html = '';
            $.each(l2.result, function (index, item) {
                html += '<option>' + item.classification + '</option>';
            });
            $('#mod_classification').html(html);
            $('#mod_classification').val(res.commodity_classification)
        });
        $.each(res.p, function (index, item) {
            html += '<div><input type="radio" name="property" value="' + item.property_id + '">' +
                '<input type="text" class="input" name="property" vv="' + item.property_id + '" value="' + item.property + '"></div>';
        });
        $('#mod_property').html(html);
        $('input[type="radio"][name="property"]').click(function () {
            var n = $('input[type="radio"][name="property"]:checked').val();
            html = '';
            $(res.p).each(function (index, obj) {
                if (obj.property_id == n) {
                    $(obj.v).each(function (index, pro) {
                        html += '<div><input type="text" name="property_value" class="input" vv="' + pro.value_id + '" value="' + pro.value + '"></div>'
                    });
                }
            });
            $('#mod_property_value').html(html);
        })
    });
});
function mod_save() {
    var com_id = getQueryString('id');
    var com_name = $('#mod_name').val();
    var com_classification = $('#mod_classification').val();
    var com_price = $('#mod_price').val();
    var com_preferential = $('#mod_preferential').val();
    var com = {
        commodity_id: com_id,
        commodity_name: com_name,
        commodity_classification: com_classification,
        commodity_price: com_price,
        commodity_preferential: com_preferential
    };
    $.post('/commodity/modify_commodity', com, function (data) {
    });
    $.each($('input[type=text][name=property]'), function (index, item) {
        var property_id = $(item).attr('vv');
        var property = $(item).val();
        $.post('/commodity/modify_property', {property_id: property_id, com_property: property}, function (data) {
        });
    });
    $.each($('input[type=text][name=property_value]'), function (index, item) {
        var value_id = $(item).attr('vv');
        var property_value = $(item).val();
        $.post('/commodity/modify_property_value', {
            value_id: value_id,
            property_value: property_value
        }, function (data) {
        })
    });
}